/*
 *  (C) Copyright 2007 Insula Tecnologia da Informacao Ltda.
 * 
 *  This file is part of Jazz.
 *
 *  Jazz is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Jazz is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Jazz.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.jazz.resolver.echo2;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

import java.util.LinkedList;
import java.util.List;

import nextapp.echo2.app.ListBox;
import nextapp.echo2.app.list.AbstractListModel;
import nextapp.echo2.app.list.ListSelectionModel;

import org.junit.Test;

public class DefaultListBoxResolverTestCase {

	private ListBox listBox = new ListBox();

	private DefaultListBoxResolver resolver = new DefaultListBoxResolver();

	@Test
	public void testGetValueWithArray() {
		listBox = new ListBox(new String[] { "banana", "apple", "watermelon", "cashew", "avocado", "papaya" });

		listBox.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listBox.setSelectedIndex(2);
		assertEquals("watermelon", resolver.getValue(listBox));

		listBox.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_SELECTION);
		listBox.setSelectedIndices(new int[] { 0, 3, 4 });
		assertArrayEquals(new Object[] { "banana", "cashew", "avocado" }, (Object[]) resolver.getValue(listBox));
	}

	@Test
	public void testSetValueWithArray() {
		listBox = new ListBox(new String[] { "banana", "apple", "watermelon", "cashew", "avocado", "papaya" });

		listBox.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		resolver.setValue(listBox, "papaya");
		assertEquals(5, listBox.getSelectedIndices()[0]);

		listBox.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_SELECTION);
		resolver.setValue(listBox, new String[] { "papaya", "banana", "avocado" });
		assertArrayEquals(new int[] { 0, 4, 5 }, listBox.getSelectedIndices());
	}

	@Test
	public void testGetValueWithCollection() {
		listBox = new ListBox(new MyListModel());

		listBox.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listBox.setSelectedIndex(0);
		assertEquals("porsche", resolver.getValue(listBox));

		listBox.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_SELECTION);
		listBox.setSelectedIndices(new int[] { 1, 4 });
		assertArrayEquals(new Object[] { "ferrari", "jaguar" }, (Object[]) resolver.getValue(listBox));
	}

	@Test
	public void testSetValueWithCollection() {
		listBox = new ListBox(new MyListModel());

		listBox.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		resolver.setValue(listBox, "jaguar");
		assertEquals(4, listBox.getSelectedIndices()[0]);

		listBox.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_SELECTION);

		List<String> values = new LinkedList<String>();
		values.add("aston martin");
		values.add("jaguar");
		values.add("ferrari");
		resolver.setValue(listBox, values);

		assertArrayEquals(new int[] { 1, 3, 4 }, listBox.getSelectedIndices());
	}

	private class MyListModel extends AbstractListModel {

		private static final long serialVersionUID = 1L;

		private List<String> elementList = new LinkedList<String>();

		public MyListModel() {
			elementList.add("porsche");
			elementList.add("ferrari");
			elementList.add("bugatti");
			elementList.add("aston martin");
			elementList.add("jaguar");
		}

		@Override
		public Object get(int index) {
			return elementList.get(index);
		}

		@Override
		public int size() {
			return elementList.size();
		}
	}

}
